//
//  LoginView.m
//  specialine
//
//  Created by 往返网络 on 17/2/10.
//  Copyright © 2017年 com. All rights reserved.
//

#import "LoginView.h"

#define fontsize 15

#define LOGIN_Normal 1000 //正常登陆
#define LOGIN_Canot 2000  //无法登陆，弹出alertviewcontroller

#define USERNAME @"username"

#define PASSWORD @"password"

@interface LoginView ()<UITextFieldDelegate>

@end

@implementation LoginView

{
    
    UITextField * phonetf;
    
    UITextField * passwordtf;
    
    UIButton * loginbtn; // 1000
    
    UIButton * resetlogin; //无法登录 2000
    
    UIButton * zhucebtn; //去注册 3000
    
    UIImageView * logoimageview; //logo图标
    
    UIImageView * backgroundimage; //页面底部背景图
    
}

- (instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        
        [self createUI];
        
    }


    return self;

}



- (void)createUI{
    
    //适配字体大小,圆角大小
    
    float fontsie = 15;
    
    float cornersize = 20;
    
    fontsie = SCREEN_H < 667 ? 13 :  17;
    
    cornersize = SCREEN_H < 667 ? 18 : 22 ;
    
    
    
    if (SCREEN_H == 667) {
        
        fontsie = 15;
        
        cornersize = 20;
        
        
    }
    
    self.backgroundColor = [UIColor whiteColor];
    
    UILabel * titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, SCREEN_W, 44)];
    
    titlelabel.textAlignment = NSTextAlignmentCenter;
    
    titlelabel.text = @"登录";
    
    titlelabel.font = [UIFont systemFontOfSize:fontsie+2];
    
    [self addSubview:titlelabel];
    
    
    logoimageview = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_W*0.5 - 104.0/750*SCREEN_W*0.5, 185.0/1334.0*SCREEN_H, 104.0/750*SCREEN_W, 83.0/1334*SCREEN_H)];
    
    logoimageview.image = [UIImage imageNamed:@"LoginIcon.png"];
    
    [self addSubview:logoimageview];
    
    //手机号。。密码 输入框创建
    
    phonetf = [[UITextField alloc]initWithFrame:CGRectMake(65.0/W_BASE*SCREEN_W, CGRectGetMaxY(logoimageview.frame)+62.0/H_BASE*SCREEN_H, 605.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    
    phonetf.layer.borderWidth = 1;
    
    phonetf.font = [UIFont systemFontOfSize:fontsie];
    
    //设置tf的缩紧，靠右缩紧，给tf加一个leftview
    
    UIView * leftview  = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    
    phonetf.leftView = leftview;
    
    phonetf.leftViewMode = UITextFieldViewModeAlways;
    
    phonetf.layer.borderColor = [[UIColor colorWithRed:239.0/255 green:239.0/255 blue:239.0/255 alpha:1]CGColor];
    
    phonetf.placeholder = @"手机号";
    
    phonetf.clearButtonMode = UITextFieldViewModeAlways;
    
    phonetf.layer.cornerRadius = cornersize;
    
    [self addSubview:phonetf];
    
    
    passwordtf = [[UITextField alloc]initWithFrame:CGRectMake(65.0/W_BASE*SCREEN_W, CGRectGetMaxY(phonetf.frame)+20.0/H_BASE*SCREEN_H, 605.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    
    
    passwordtf.layer.borderWidth = 1;
    
    UIView * leftview1  = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    
    passwordtf.leftView = leftview1;
    
    passwordtf.font = [UIFont systemFontOfSize:fontsie];
    
    passwordtf.leftViewMode = UITextFieldViewModeAlways;
    
    passwordtf.layer.borderColor = [[UIColor colorWithRed:239.0/255 green:239.0/255 blue:239.0/255 alpha:1]CGColor];
    
    passwordtf.placeholder = @"密码";
    
    passwordtf.clearButtonMode = UITextFieldViewModeAlways;
    
    
    passwordtf.layer.cornerRadius = cornersize;
    
    [self addSubview:passwordtf];
    
    passwordtf.secureTextEntry = YES;
    
    passwordtf.delegate = self;
    
    phonetf.delegate = self;
    
    
    loginbtn = [[UIButton alloc]initWithFrame:CGRectMake(65.0/W_BASE*SCREEN_W, CGRectGetMaxY(passwordtf.frame)+20.0/H_BASE*SCREEN_H, 605.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    
    loginbtn.backgroundColor = [UIColor colorWithRed:47.0/255 green:148.0/255 blue:251.0/255 alpha:0.5];
    
    [loginbtn setTitle:@"登录" forState:UIControlStateNormal];
    
    loginbtn.userInteractionEnabled = NO;
    
    loginbtn.layer.cornerRadius = cornersize;
    
    loginbtn.titleLabel.font = [UIFont systemFontOfSize:fontsie];
    
    loginbtn.tag = 1000;
    
    [self addSubview:loginbtn];
    
    
    [loginbtn addTarget:self action:@selector(enterLogin:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //无法登录（忘记密码）
    
    resetlogin = [[UIButton alloc]initWithFrame:CGRectMake(90.0/W_BASE*SCREEN_W, CGRectGetMaxY(loginbtn.frame)+5.0/H_BASE*SCREEN_H, 180.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    
    [resetlogin setTitle:@"忘记密码？" forState:UIControlStateNormal];
    
    resetlogin.titleLabel.font = [UIFont systemFontOfSize:fontsie];
    
    [resetlogin setTitleColor:COLORRGBA(102, 102, 102, 1) forState:UIControlStateNormal];
    
    resetlogin.tag = 2000;
    
    [resetlogin addTarget:self action:@selector(enterLogin:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:resetlogin];
    
    
    //去注册
    
    zhucebtn = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(loginbtn.frame)-130.0/W_BASE*SCREEN_W, CGRectGetMaxY(loginbtn.frame)+5.0/H_BASE*SCREEN_H, 100.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H)];
    
    [zhucebtn setTitle:@"去注册" forState:UIControlStateNormal];
    
    zhucebtn.titleLabel.font = [UIFont systemFontOfSize:fontsie];
    
    [zhucebtn setTitleColor:COLORRGBA(102, 102, 102, 1) forState:UIControlStateNormal];
    
    zhucebtn.tag = 3000;
    
    [zhucebtn addTarget:self action:@selector(enterLogin:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:zhucebtn];
    
    //设置背景 (689,204)
    backgroundimage = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_W*0.5-689*0.5*0.5, SCREEN_H-204*0.5-2, 689*0.5, 204*0.5 )];
    
    backgroundimage.image = [UIImage imageNamed:@"LoginImage.png"];
    
    [self addSubview:backgroundimage];
    
}

#pragma mark   - -- - - -- - 调用代理协议方法（登陆／ 无法登录）- - - - -- - - -- - -
- (void)enterLogin:(UIButton *)loginbtntag{

    
    NSInteger type = loginbtntag.tag;
    
    NSDictionary * dict = @{USERNAME:phonetf.text,PASSWORD:passwordtf.text};
    
    switch (type) {
            

        case 1000:
            
            //播放登陆动画
        {
           
            //正常登陆 ,调用代理方法
            
            [self.loginviewDelgate loginViewClickButtonOfType:LoginViewButtonTypeOfNormalLogin withDictionary:dict];
            

            break;
            
        }
            
            case 2000:
            
            //无法登陆，弹出alertViewController,调用代理方法
            
            [self.loginviewDelgate loginViewClickButtonOfType:LoginViewButtonTypeOfCannotLogin withDictionary:nil];
            
            break;
            
            case 3000:
            
            //没有帐号，去注册
            
            NSLog(@"进入注册页面");
            
            [self.loginviewDelgate loginViewClickButtonOfType:LoginViewButtonTypeOfRegister withDictionary:nil];


        default:
            
            break;
            
            
            
    }


}



- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    //一旦有输入1个字符，就变色
    
    NSString * textstring = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    
    if (textstring.length >= 1) {
        
        loginbtn.backgroundColor = [UIColor colorWithRed:47.0/255 green:148.0/255 blue:251.0/255 alpha:1];
        
        loginbtn.userInteractionEnabled = YES;
        
    }


    //下方为输入手机号满11位，密码满6位时，登陆按钮变色
    
//    NSString * textstring = [textField.text stringByReplacingCharactersInRange:range withString:string];
//    
//    
//    if ((textstring.length >= 11 && textField == phonetf) && (passwordtf.text.length == 6)) {
//        
//        
//        loginbtn.backgroundColor = [UIColor colorWithRed:47.0/255 green:148.0/255 blue:251.0/255 alpha:1];
//        
//        loginbtn.userInteractionEnabled = YES;
//        
//        
//    }else if ((textstring.length >=6 && textField == passwordtf) && (phonetf.text.length == 11)){
//        
//        loginbtn.backgroundColor = [UIColor colorWithRed:47.0/255 green:148.0/255 blue:251.0/255 alpha:1];
//        
//        loginbtn.userInteractionEnabled = YES;
//        
//        
//    }
    
    return YES;
    
    
    
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{


    NSLog(@"页面整体上移");
    
    [self makeUPView];
    
    return YES;
    

}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [self endEditing:YES];
    
    
    return YES;


}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    
      // 一旦编辑内容，页面整体向上弹 。。显示图片
    
   
        [self makeDownView];
    
        [phonetf resignFirstResponder];
    
        [passwordtf resignFirstResponder];
    
    
}

#pragma mark - - -- - - 页面整体上移－ － － － －

- (void)makeUPView{

    

    [UIView animateWithDuration:0.5 animations:^{
        

        CGRect rect1 = CGRectMake(SCREEN_W*0.5 - 104.0/750*SCREEN_W*0.5, -86.0/1334.0*SCREEN_H, 104.0/750*SCREEN_W, 83.0/1334*SCREEN_H);
        
        logoimageview.frame = rect1;
        
        
        CGRect rect2 = CGRectMake(65.0/W_BASE*SCREEN_W, 148.0/H_BASE*SCREEN_H, 605.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H);
        
        phonetf.frame = rect2;
        
        CGRect rect3 = CGRectMake(65.0/W_BASE*SCREEN_W, CGRectGetMaxY(phonetf.frame)+20.0/H_BASE*SCREEN_H, 605.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H);
        
        passwordtf.frame = rect3;
        
        CGRect  rect4 = CGRectMake(65.0/W_BASE*SCREEN_W, CGRectGetMaxY(passwordtf.frame)+20.0/H_BASE*SCREEN_H, 605.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H);
        
        loginbtn.frame = rect4;
        
        
        CGRect rect5 = CGRectMake(95.0/W_BASE*SCREEN_W, CGRectGetMaxY(loginbtn.frame)+5.0/H_BASE*SCREEN_H, 180.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H);
        
        resetlogin.frame = rect5;
        
        CGRect rect6 = CGRectMake(CGRectGetMaxX(loginbtn.frame)-130.0/W_BASE*SCREEN_W, CGRectGetMaxY(loginbtn.frame)+5.0/H_BASE*SCREEN_H, 100.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H);
        
        zhucebtn.frame = rect6;
        
        
        [backgroundimage removeFromSuperview];
        
        
    } completion:nil];
    

}

#pragma mark - - - - -- -  页面整体下移 － － － －－ － －

- (void)makeDownView{


    [UIView animateWithDuration:0.5 animations:^{
        
        
        
        CGRect rect1 = CGRectMake(SCREEN_W*0.5 - 104.0/750*SCREEN_W*0.5, 185.0/1334.0*SCREEN_H, 104.0/750*SCREEN_W, 83.0/1334*SCREEN_H);

        logoimageview.frame = rect1;
        
        CGRect rect2 = CGRectMake(65.0/W_BASE*SCREEN_W, CGRectGetMaxY(logoimageview.frame)+62.0/H_BASE*SCREEN_H, 605.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H);
        
        phonetf.frame = rect2;
        
        CGRect rect3 = CGRectMake(65.0/W_BASE*SCREEN_W, CGRectGetMaxY(phonetf.frame)+20.0/H_BASE*SCREEN_H, 605.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H);
        
        passwordtf.frame = rect3;
        
        CGRect  rect4 = CGRectMake(65.0/W_BASE*SCREEN_W, CGRectGetMaxY(passwordtf.frame)+20.0/H_BASE*SCREEN_H, 605.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H);
        
        loginbtn.frame = rect4;
        
        
        CGRect rect5 = CGRectMake(95.0/W_BASE*SCREEN_W, CGRectGetMaxY(loginbtn.frame)+5.0/H_BASE*SCREEN_H, 180.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H);
        
        resetlogin.frame = rect5;
        
        CGRect rect6 = CGRectMake(CGRectGetMaxX(loginbtn.frame)-130.0/W_BASE*SCREEN_W, CGRectGetMaxY(loginbtn.frame)+5.0/H_BASE*SCREEN_H, 100.0/W_BASE*SCREEN_W, 80.0/H_BASE*SCREEN_H);
        
        zhucebtn.frame = rect6;
        

        [self addSubview:backgroundimage];
        
        
    } completion:nil];


}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
